//
//  DownloadSectionVC.m
//  JW_BasePorject
//
//  Created by JW on 2018/7/2.
//  Copyright © 2018年 JW. All rights reserved.
//

#import "DownloadSectionVC.h"

#import <BJPlayerManagerCore/BJPlayerManagerCore.h>

#import <BJPlaybackUI/BJPlaybackUI.h>

#import "PMDownloadModel+ZYVideo.h"

#import "BJYCacheVideoCell.h"

@interface DownloadSectionVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *sectionTableView;

@property (nonatomic, strong)NSMutableArray *sectionList;

@end

@implementation DownloadSectionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.sectionTableView];
    
    [self initSectionData];
    
}

- (void)initSectionData{
    NSMutableArray *finishedList = [NSMutableArray array];
    if([[PMDownloadManager downloadManager].finishedList count]) {
        finishedList = [NSMutableArray arrayWithArray:[PMDownloadManager downloadManager].finishedList];
    }
    
//    __block NSMutableArray *groupArr = [NSMutableArray array];
    NSString *groupTitle = [NSString stringWithFormat:@"%@-%@",self.courseName,self.chapterName];
    [finishedList enumerateObjectsUsingBlock:^(PMDownloadModel * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"showFileName CONTAINS [cd] %@",groupTitle];
        NSArray *tempArr = [NSArray arrayWithArray:[finishedList filteredArrayUsingPredicate:predicate]];
        self.sectionList =tempArr.mutableCopy;
    }];
    
    [self.sectionList sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        PMDownloadModel *per1 = obj1;
        
        PMDownloadModel *per2 = obj2;
        
        if (per1.indexID > per2.indexID) {
            return NSOrderedDescending;//降序
        }else if (per1.indexID < per2.indexID){
            return NSOrderedAscending;//升序
        }else{
            return NSOrderedSame;//相等
        }
    }];
    
    [self.sectionTableView reloadData];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sectionList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BJYCacheVideoCell *cell = [BJYCacheVideoCell cellWithTableView:tableView];
    cell.PM_model = self.sectionList[indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
}


#pragma mark - property
- (UITableView *)sectionTableView{
    if (_sectionTableView == nil) {
        _sectionTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBarHeight-36) style:UITableViewStylePlain];
        _sectionTableView.delegate = self;
        _sectionTableView.dataSource = self;
        [_sectionTableView registerClass:[BJYCacheVideoCell class] forCellReuseIdentifier:@"BJYCacheVideoCell"];
        _sectionTableView.rowHeight = UITableViewAutomaticDimension;
        // 设置tableView的估算高度
        _sectionTableView.estimatedRowHeight = 60;
        _sectionTableView.tableFooterView = [UIView new];
        
    }
    return _sectionTableView;
}

- (NSMutableArray *)sectionList{
    if (_sectionList == nil) {
        _sectionList = [NSMutableArray array];
    }
    return _sectionList;
}

@end
